package com.jwl.idc.customview;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface.OnClickListener;
import android.text.TextUtils;
import android.view.View;

import java.util.HashMap;

public class DialogCreater {

    private static HashMap<Integer, Dialog> dialogMap = new HashMap<Integer, Dialog>();

    private static int dialogFlag = 0;

    public static HashMap<Integer, Dialog> getDialogMap() {
        return dialogMap;
    }

    public static Dialog getDialog(Context context) {
        String className = context.toString();

        if (!dialogMap.containsKey(className)) {
            return null;
        }

        return dialogMap.get(className);
    }

    public static CustomDialog showDialog(Context context, String title,
                                          String message, final View dialogView, OnClickListener positiveButtonClickListener,
                                          OnClickListener negativeButtonClickListener,
                                          final int dialogId, String positive, String negative) {
        CustomDialog dialog = new CustomDialog(context);
        dialog.setTitle(title);
        dialog.setMessage(message);
        dialog.setContentView(dialogView);
        dialog.setPositiveButtonText(positive);
        dialog.setNegativeButtonText(negative);
        dialog.setDialogId(dialogId);

        if (TextUtils.isEmpty(positive) && TextUtils.isEmpty(negative)) {
            dialog.setHasBottom(false);
        } else {
            dialog.setHasBottom(true);
        }

        dialog.setPositiveButtonClickListener(positiveButtonClickListener);
        dialog.setNegativeButtonClickListener(negativeButtonClickListener);
        CustomDialog rDialog = dialog.create(context);
        dialogMap.put(dialogId, rDialog);
        rDialog.setDialogId(dialogId);
        rDialog.setContentView(dialogView);
        return rDialog;
    }

    public static void closeDialog(int dialogId) {

        if (!dialogMap.containsKey(dialogId)) {
            return;
        }

        Dialog dialog = dialogMap.get(dialogId);
        dialogMap.remove(dialogId);
        if ((dialog != null) && (dialog.getWindow() != null) && dialog.isShowing()) {
            dialog.dismiss();
            dialog = null;
        }
    }
}
